#!/bin/bash
# variables
# function
#设置环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DM_BIN
export PATH=$PATH:$DM_BIN

ip=SYSDBA/${SYSDBA_PWD}@${DB_HOST1}:${DB_PORT1}
props=props.dm

LOGDIR=`pwd`/log/01_tpcc
LOG=$LOGDIR/$filename"_"$TABLE_TYPE"_"$WAREHOUSES"w_"`date "+%Y%m%d%H%M%S"`.log
SCRIPTS_DIR=`pwd`/01_tpcc
# new create log directory
if [ ! -d ${LOGDIR} ]; then 
  mkdir -p ${LOGDIR}
fi

cd ${SCRIPTS_DIR}/tpcc
sed -i "/^CREATE TABLESPACE BENCHMARKSQL1 DATAFILE 'BENCHMARKSQL1.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL1 DATAFILE 'BENCHMARKSQL1.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user1.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL1 DATAFILE 'BENCHMARKSQL1.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL1 DATAFILE 'BENCHMARKSQL1.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL2 DATAFILE 'BENCHMARKSQL2.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL2 DATAFILE 'BENCHMARKSQL2.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL3 DATAFILE 'BENCHMARKSQL3.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL3 DATAFILE 'BENCHMARKSQL3.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL4 DATAFILE 'BENCHMARKSQL4.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL4 DATAFILE 'BENCHMARKSQL4.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL5 DATAFILE 'BENCHMARKSQL5.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL5 DATAFILE 'BENCHMARKSQL5.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL6 DATAFILE 'BENCHMARKSQL6.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL6 DATAFILE 'BENCHMARKSQL6.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL7 DATAFILE 'BENCHMARKSQL7.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL7 DATAFILE 'BENCHMARKSQL7.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL8 DATAFILE 'BENCHMARKSQL8.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL8 DATAFILE 'BENCHMARKSQL8.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL9 DATAFILE 'BENCHMARKSQL9.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL9 DATAFILE 'BENCHMARKSQL9.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql
sed -i "/^CREATE TABLESPACE BENCHMARKSQL10 DATAFILE 'BENCHMARKSQL10.dbf' SIZE/c CREATE TABLESPACE BENCHMARKSQL10 DATAFILE 'BENCHMARKSQL10.dbf' SIZE $TBS_SIZE AUTOEXTEND ON NEXT 2048;" bms5_create_user.sql

#runBenchmark
cd ${SCRIPTS_DIR}/tpcc/benchmarksql5/run/
cp props.dm preheat.dm
sed -i "/^warehouses/c warehouses=$WAREHOUSES" $props 
sed -i "/^loadWorkers/c loadWorkers=$LOADWORKERS" $props 
sed -i "/^conn/c conn=jdbc:dm://${DB_HOST1}:${DB_PORT1}" $props 

starttime=`date +'%Y-%m-%d %H:%M:%S'`
echo "---BEGIN---"|tee -a ${LOG}
echo "!!!tpcc start: $starttime"|tee -a ${LOG}
echo "!!!check db info"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_checkdb.sql
exit
EOF
echo ""|tee -a ${LOG}
echo "!!!create table!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/$usersql
start ${SCRIPTS_DIR}/tpcc/$tablesql
exit
EOF
starttime1=`date +'%Y-%m-%d %H:%M:%S'`
echo ""|tee -a ${LOG}
echo "!!!import data!!!"|tee -a ${LOG}
echo "!!!time: $starttime1"|tee -a ${LOG}
echo ""|tee -a ${LOG}
./runLoader.sh $props |tee -a ${LOG}
echo ""|tee -a ${LOG}
echo "!!!add indexes!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_index.sql
start ${SCRIPTS_DIR}/tpcc/bms5_ckpt.sql
exit
EOF
endtime1=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds1=$(date --date="$starttime1" +%s);
end_seconds1=$(date --date="$endtime1" +%s);

echo ""|tee -a ${LOG}
echo "!!!check data!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_count.sql
exit
EOF
echo ""|tee -a ${LOG}
echo "!!!load data used:"$((end_seconds1-start_seconds1))"s" |tee -a $LOG
rm -rf dameng_result*
rm -rf dmsql*